//app.js
App({
  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    var that = this;
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function(res) {
        var long = res.longitude + 0.015658,
          lat = res.latitude - 0.003299
        // console.log("定位成功", long);
        console.log("定位成功", res.latitude, res.longitude);
        var locationString = lat + "," + long;
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1',
          data: {
            "key": "YLFBZ-WHAWI-ZXUGH-53Q65-TOJ7E-ADBNQ",
            "location": locationString
          },
          method: 'GET',
          // header: {}, 
          success: function(res) {
            // success
            console.log("请求成功");
            console.log("请求数据:" + res.data.result.address);
            that.globalData.address = res.data.result.address
          },
          fail: function() {
            // fail
            console.log("请求失败");
          },
          complete: function() {
            // complete
            console.log("请求完成");
          }
        })
      },
      fail: function() {
        // fail
        console.log("用户拒绝授权");
      },
      complete: function() {
        // complete
        console.log("定位工作完成");
      }
    })
    
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              wx.login({
                success: res => {
                  console.log(res.code)
                  wx.request({
                    url: 'https://api.tanghaolun.cn/xxd_login',
                    method: 'POST',
                    data: {
                      "code": res.code
                    },
                    success: function(result) {
                      console.log(result, 1)
                      if (result.data.status == 300) {
                        // console.log(result.data.msg)
                        that.globalData.status = result.data.status
                        that.globalData.msg = result.data.msg
                        that.globalData.interal = result.data.interal
                      } else {
                        that.globalData.status = result.data.status
                        that.globalData.msg = result.data.msg
                        that.globalData.interal = result.data.interal
                      }
                    },
                    fail: function() {
                      console.log("ajax error")
                    }
                  })
                }
              })
              // this.globalData.status = 301
              // that.globalData.interal = 1

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              // if (this.userInfoReadyCallback) {
              //   this.userInfoReadyCallback(res)
              //   console.log(res)
              // }
            }
          })
        } else {
          // console.log(that.globalData.res_code)
          wx.login({
            success: res => {
              console.log(res.code)
              wx.request({
                url: 'https://api.tanghaolun.cn/xxd_login',
                method: 'POST',
                data: {
                  "code": res.code
                },
                success: function(result) {
                  console.log(result, 12)
                  if (result.data.status == 300) {
                    // console.log(result.data.msg)
                    that.globalData.status = result.data.status
                    that.globalData.msg = result.data.msg
                    that.globalData.interal = result.data.interal
                  } else {
                    that.globalData.status = result.data.status
                    that.globalData.msg = result.data.msg
                    that.globalData.interal = result.data.interal
                  }
                },
                fail: function() {
                  console.log("ajax error")
                }
              })
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    interal: null,
    msg: null,
    status: null,
    address: null
  },
  startOperating: function(info) {
    wx.showLoading({
      title: info,
      mask: true
    })
  },
  showSuccess: function(message) {
    wx.showToast({
      title: message,
      duration: 2000,
      image: "/images/Common/Success.png",
    })
  },
  // 错误      
  showError: function(message) {
    wx.showToast({
      title: message,
      duration: 2000,
      image: "/images/Common/Error.png",
    })
  },
  // 警告   
  showWarn: function(message) {
    wx.showToast({
      title: message,
      duration: 2000,
      image: "/images/Common/Warn.png",
    })
  }
})